<template>
    <!-- 使用flex布局的行，设置内容居中对齐，宽度为100% -->
    <el-row type="flex" justify="center" align="middle" style="width: 100%;">
        <!-- 使用占满24栅格宽度的列，即全宽 -->
        <el-col :span="24">
            <!-- 头部布局，左右对�?-->
            <el-row class="header_home" justify="space-between">
                <!-- 左侧：显示图�?-->
                <el-image style="width: auto; height: 60px; margin-left: 1%;" :src="imgUrl" />
                <!-- 中间：搜索框 -->
                <el-input v-model="takeoutName" style="width: 50%;height: 50%;" placeholder="搜索商品..." size="large">
                    <!-- 搜索框内的附加按钮：搜索图标 -->
                    <template #append>
                        <el-button :icon="Search" @click="searchTakeoutByName" />
                    </template>
                </el-input>
                <!-- 右侧：未登录时显示登录按钮，登录后显示用户菜单 -->
                <el-link v-show="!isLogin"
                    style="color: white;font-size: large; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    padding: 10px 24px; border-radius: 12px; margin-right: 20px; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
                    transition: all 0.3s ease;"
                    :underline="false" @click="login">
                    登录
                </el-link>
                <el-menu v-show="isLogin"
                    style="background-color: transparent;border-bottom: none;text-decoration: none;" mode="horizontal"
                    :ellipsis="false" background-color="transparent" text-color="#ffffff">
                    <!-- 用户菜单，包含头像、用户名、个人信息和退出登录 -->
                    <el-sub-menu style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
                    border-radius: 12px; padding: 4px 12px; box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);">
                        <template #title>
                            <el-image style="height: 55px;width: 55px;border-radius: 50px;margin-right: 10px;"
                                :src="imageUrl" />
                            <el-text>{{ userStore.username }}</el-text>
                        </template>
                        <el-menu-item @click="informationClick">个人信息</el-menu-item>
                        <el-menu-item @click="logoutClick">退出登录</el-menu-item>
                    </el-sub-menu>
                </el-menu>
            </el-row>
        </el-col>
        <!-- 轮播图区域 -->
        <el-col :span="24">
            <el-carousel height="400px">
                <el-carousel-item v-for="item in recommentTakeouts" :key="item.id">
                    <el-image style="width: 100%;" :src="`${item.takeout_portrait}`" fit="cover"
                        @click="goTakeout(item.id!)" />
                </el-carousel-item>
            </el-carousel>
        </el-col>
        <el-col :span="22">
            <el-divider content-position="left">
                <el-text style="font-size: large;font-weight: bold; margin-top: 1%;">
                    分类
                </el-text>
            </el-divider>
            <el-space wrap fill-ratio="" fill class="kind_body">
                <el-row v-for="item in kind" :key="item.id">
                    <el-col :span="24" align="center">
                        <el-link style=" font-size: large;" @click="searchTakeoutByType(item.id!)">
                            {{ item.type }}
                        </el-link>
                    </el-col>
                </el-row>
            </el-space>
        </el-col>
        <!-- 新品推荐区域 -->
        <el-col :span="22">
            <el-divider content-position="left">
                <el-text style="font-size: x-large;font-weight: bold;">
                    新品栏目
                </el-text>
            </el-divider>
        </el-col>
        <el-col :span="22" style="margin-top: 2%;">
            <!-- 展示新品外卖 -->
            <el-space wrap fill-ratio="" fill>
                <div v-for="i in latestTakeout" :key="i.id">
                    <el-card shadow="hover" style="width: 250px;height: 400px;border-radius: 15px;margin-top: 5%;"
                        @click="goTakeout(i.id!)">
                        <!-- 卡片内容：图片、名称、简介、价格、浏览量和评分 -->
                        <el-row align="middle" justify="center">
                            <el-col :span="24">
                                <el-image :src="`${i.takeout_portrait}`" style="width: 100%;height: 200px;"
                                    fit="contain" />
                            </el-col>
                            <el-col :span="24" align="start">
                                <el-text style="font-size: larger;" line-clamp="1">{{ i.takeout_name }}</el-text>
                            </el-col>
                            <el-col :span="24" align="start">
                                <el-text style="color: gray;height: 60px;" line-clamp="3">{{ i.introduce }}</el-text>
                            </el-col>
                            <el-col :span="24" align="end">
                                <el-text style="color: red;font-size: large;">¥ {{ i.price }}</el-text>
                            </el-col>
                            <el-col :span="24" style="margin-top: 5%;">
                                <el-row style="width: 100%;">
                                    <el-col :span="24" align="start">
                                        <el-text>浏览量：{{ i.flow }}</el-text>
                                        <el-text style="margin-left: 10%;">评分：{{ i.mark }}</el-text>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </el-card>
                </div>
            </el-space>
        </el-col>
        <!-- 甄选推荐区域 -->
        <el-col :span="22" style="margin-top: 2%;">
            <el-divider content-position="left">
                <el-text style="font-size: x-large;font-weight: bold;">
                    甄选推荐
                </el-text>
            </el-divider>
        </el-col>
        <el-col :span="22" style="margin-bottom: 5%;">
            <!-- 引入甄选推荐组件 -->
            <Takeouts />
        </el-col>
        <!-- 返回顶部按钮 -->
        <el-backtop :bottom="100" :right="20" style="
        height: 50px;
        width: 50px;
        background-color: #fbaf5a;
        box-shadow: var(--el-box-shadow-lighter);
        text-align: center;
        line-height: 30px;
        color: #ffffff;
        border-radius: 50px;
      ">
        </el-backtop>
    </el-row>
</template>


<script lang="ts" setup>
// 引入Vue相关的函数和组件
import { ref, onMounted } from 'vue';
import { Search } from '@element-plus/icons-vue';// 引入Element Plus的Search图标
import { ElDivider } from 'element-plus';// 引入Element Plus的分隔符组件

// 引入Vuex存储（假设用于用户信息）
import { userInfoStore } from '../assets/userStore/userStore.ts';
// 引入Vue Router
import { useRouter } from 'vue-router';

// 引入自定义的模型和API
import { TakeoutModel } from '../assets/models/Takeout.ts';
import Takeouts from '../components/Takeouts.vue';
import { agcApis } from '../assets/apis/AgcApis.ts';
import { UserModel } from '../assets/models/User.ts';
import { userApis } from '../assets/apis/UserApis.ts';
import { takeoutApis } from '../assets/apis/TakeoutApis.ts';
import { KindModel } from '../assets/models/Kind.ts';
import { imgUrl } from '../assets/common/Constants.ts';

// 引入AGC认证异常和错误类型
import { AGCAuthException } from '@hw-agconnect/auth';
import { AGCError } from '@hw-agconnect/instance';
import { kindApis } from '../assets/apis/KindApis.ts';



// 初始化Vuex存储实例
const userStore = userInfoStore()
// 初始化Vue Router实例
const router = useRouter();
// 响应式变量定义
const isLogin = ref<boolean>(false)// 登录状态
const takeoutName = ref<string>()
const recommentTakeouts = ref<Array<TakeoutModel>>();// 推荐外卖列表
const takeouts = ref<Array<TakeoutModel>>();// 所有外卖列表
const latestTakeout = ref<Array<TakeoutModel>>();
const imageUrl = ref<string>();// 用户头像URL
const kind = ref<Array<KindModel>>();

onMounted(() => {
    setTimeout(() => {
        // 获取当前用户信息
        agcApis.getCurrectUser()
            .then((user) => {
                // 根据用户是否存在更新登录状态
                isLogin.value = user === null ? false : true;
                if (user) {
                    // 查询用户详细信息并更新Vuex存储
                    userApis.queryUser(userStore.id)
                        .then((mine: UserModel) => {
                            userStore.update(user.getUid(), mine.username, mine.phone, mine.head_portrait)
                            // 根据用户头像是否存在设置头像URL
                            imageUrl.value = mine.head_portrait ? `${mine.head_portrait}` : 'src/assets/resource/ic_user_portrait.svg';
                        })
                }
            })
            .catch((error) => {
                // 捕获并处理AGC认证异常或错误
                if (error instanceof AGCAuthException || error instanceof AGCError) {
                    // 根据错误码进行处理
                    console.log(JSON.stringify(error.message));
                }
            })
    }, 200)// 延迟200毫秒执行

    // 获取推荐的外卖列表
    takeoutApis.recommendTakeouts()
        .then((value: Array<TakeoutModel>) => {
            recommentTakeouts.value = value;
        })

    // 获取所有的外卖列表
    takeoutApis.queryAllTakeout()
        .then((value: Array<TakeoutModel>) => {
            if (value) {
                takeouts.value = value;
            }
        })

    takeoutApis.queryLatestTakeout()
        .then((value: Array<TakeoutModel>) => {
            if (value) {
                latestTakeout.value = value;
            }
        })

    kindApis.queryKind()
        .then((value: Array<KindModel>) => {
            if (value) {
                kind.value = value;
            }
        })
})


// 导航到登录页面
const login = () => {
    router.push('/login')
}

// 导航到指定外卖详情页面
const goTakeout = (id: number) => {
    router.push({
        path: '/takeout',
        query: {
            id: id
        }
    })
}

// 根据名称搜索外卖
const searchTakeoutByName = () => {
    router.push({
        path: '/search',
        query: {
            way: 0,
            arg: takeoutName.value
        }
    })
}

// 根据类型搜索外卖
const searchTakeoutByType = (id: number) => {
    router.push({
        path: '/search',
        query: {
            way: 1,
            arg: id
        }
    })
}

// 用户注销逻辑
const logoutClick = () => {
    agcApis.logOut();
    location.reload();// 重新加载页面
}

// 导航到用户信息页面
const informationClick = () => {
    router.push('/profile')
}
</script>
<style scoped>
.header_home {
    width: 100%;
    height: 90px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    align-items: center;
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.2);
    position: relative;
}

.header_home::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, #10B981 0%, #06B6D4 50%, #10B981 100%);
}

.kind_body {
    padding: 2%;
    background: linear-gradient(135deg, #F0F9FF 0%, #E0E7FF 100%);
    margin-top: 1%;
    margin-bottom: 1%;
    border-radius: 16px;
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.08);
}

/* 商品卡片美化 */
.el-card {
    border-radius: 16px !important;
    border: none !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08) !important;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
}

.el-card:hover {
    transform: translateY(-8px) !important;
    box-shadow: 0 12px 24px rgba(102, 126, 234, 0.2) !important;
}

/* 分类链接美化 */
.kind_body .el-link {
    padding: 12px 24px;
    border-radius: 12px;
    background: white;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    color: #4F46E5 !important;
    font-weight: 600;
}

.kind_body .el-link:hover {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white !important;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

/* 返回顶部按钮美化 */
.el-backtop {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    border-radius: 16px !important;
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4) !important;
}

/* 轮播图优化 */
.el-carousel {
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
    margin: 20px 0;
}

/* 价格标签 */
.el-text[style*="color: red"] {
    background: linear-gradient(135deg, #F59E0B 0%, #EF4444 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-weight: 700;
}

/* 搜索框美化 */
.el-input {
    border-radius: 12px !important;
}

.el-input__wrapper {
    border-radius: 12px !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05) !important;
}
</style>
